package edu.gatech.team404.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.gatech.team404.Team;
import edu.gatech.team404.dal.LabDAL;
import edu.gatech.team404.dal.TeamDAL;
import edu.gatech.team404.tool.Log;
import edu.gatech.team404.tool.StatusCode;

/**
 * Servlet implementation class Authenticate
 * @author Michael Saul
 * @version 1.0
 */
@WebServlet("/CreateTeam")
public class CreateTeam extends HttpServlet {

	/** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws NumberFormatException if a number format exception occurs
     * @throws IOException if an I/O error occurs
     */
	protected void processRequest(HttpServletRequest request, 
								  HttpServletResponse response)
								  throws IOException, NumberFormatException {
        response.setContentType("text/html;charset=UTF-8");
        final PrintWriter out = response.getWriter();
        final HttpSession session = request.getSession(true);
        final TeamDAL teamDAL = new TeamDAL();
        final String courseName = request.getParameter("courseName");
        try {
        	final String name = request.getParameter("name");
        	final String labID = request.getParameter("labID");
        	boolean isValid = true;
        	
        	//Validate data
        	if (name.isEmpty() || labID.isEmpty()) {
        		isValid = false;
        		session.setAttribute("status", false);
        	}
        	
        	if (isValid) {
	        	final Team team = new Team(labID, null, name);
	        	
	        	boolean teamCheck = teamDAL.createTeam(team);
	  
	        	if(teamCheck){
	        		session.setAttribute("createTeamStatus", true);
		        }
		        else{
		        	Log.outputVerbose();
		        	session.setAttribute("createTeamStatus", false);
		        }
        	}
        } finally {
        	
        	session.setAttribute("feed", StatusCode.CREATE.toString());
        	if (!courseName.equalsIgnoreCase("null")) {
        		response.sendRedirect("team.jsp?course=" 
            			+ URLEncoder.encode(courseName, "UTF-8"));
        	}
        	else {
        		response.sendRedirect("team.jsp");
        	}
            out.close();
        }
    }

	/** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws NumberFormatException if a number format exception occurs
     * @throws IOException if an I/O error occurs
     */
	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, NumberFormatException {
        processRequest(request, response);
    } 

	/** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws NumberFormatException if a number format exception occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws IOException, NumberFormatException {
        processRequest(request, response);
    }
    
}